﻿//Copyrigh (C) 2021 上海东欣软件工程有限公司 版权所有。
//<author>邢耀光</author>
//<data>2021.1.31</data>

using Core;
using Dal;
using System;
using System.Collections.Generic;
using System.Data;

namespace Service
{
    /// <summary>
    /// 用户信息服务类
    /// </summary>
    public class UserService
    {
        /// <summary>
        /// 用户信息操作类
        /// </summary>
        private IUserDao dao = DalService.UserDao;

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>是否添加成功</returns>
        public bool InsertUser(User user, string connString)
        {
            //检测用户姓名是否为空
            if (string.IsNullOrEmpty(user.Name))
            {
                throw new Exception("用户姓名不能为空");
            }
            //添加用户
            try
            {
                return dao.InsertUser(user, connString) > 0;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>是否修改成功</returns>
        public bool UpdateUser(User user, string connString)
        {
            //检测用户姓名是否为空
            if (string.IsNullOrEmpty(user.Name))
            {
                throw new Exception("用户姓名不能为空");
            }
            //修改用户
            try
            {
                return dao.UpdateUser(user, connString) > 0;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id">用户id</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>是否删除成功</returns>
        public bool DeleteUser(int id, string connString)
        {
            //检测用户是否为空
            if (id <= 0)
            {
                throw new Exception("用户不能为空");
            }
            //删除用户
            try
            {
                return dao.DeleteUser(id, connString) > 0;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 删除用户根据部门ID
        /// </summary>
        /// <param name="departmentID">部门id</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>影响行数，1成功，0失败</returns>
        public bool DeleteUserByDepartment(long departmentID, string connString)
        {
            //检测用户是否为空
            if (departmentID <= 0)
            {
                throw new Exception("部门不能为空");
            }
            //删除用户
            try
            {
                return dao.DeleteUserByDepartment(departmentID, connString) > 0;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 分页模糊查询用户
        /// </summary>
        /// <param name="args">用户查询信息</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <param name="page_size">页数显示大小</param>
        /// <param name="page_index">页数索引</param>
        /// <param name="totalRows">总行数</param>
        /// <returns>查询结果集</returns>
        public DataTable QueryUserPages(User args, string connString,int page_size,int page_index,out int totalRows)
        {
            //查询用户
            try
            {
                return dao.QueryUserPages(args, connString,page_size,page_index, out totalRows);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 分页精确查询用户
        /// </summary>
        /// <param name="args">用户查询信息</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <param name="page_size">页数显示大小</param>
        /// <param name="page_index">页数索引</param>
        /// <param name="totalRows">总行数</param>  
        /// <returns>查询结果集</returns>
        public DataTable ExactQueryUserPages(User args, string connString, int page_size, int page_index, out int totalRows)
        {
            //查询用户
            try
            {
                return dao.ExactQueryUserPages(args, connString, page_size, page_index, out totalRows);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 模糊查询用户
        /// </summary>
        /// <param name="args">用户查询信息</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>查询结果集</returns>
        public List<User> QueryUser(User args, string connString)
        {
            //查询用户
            try
            {
                return dao.QueryUser(args,connString);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 精确查询用户
        /// </summary>
        /// <param name="args">用户查询信息</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>查询结果集</returns>
        public List<User> ExactQueryUser(User args, string connString)
        {
            //查询用户
            try
            {
                return dao.ExactQueryUser(args, connString);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 模糊查询用户
        /// </summary>
        /// <param name="args">用户查询信息</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>查询结果集</returns>
        public DataTable QueryUserDT(User args, string connString)
        {
            //查询用户
            try
            {
                return dao.QueryUserDT(args, connString);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 精确查询用户
        /// </summary>
        /// <param name="args">用户查询信息</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>查询结果集</returns>
        public DataTable ExactQueryUserDT(User args, string connString)
        {
            //查询用户
            try
            {
                return dao.ExactQueryUserDT(args, connString);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据用户名称和所属部门id判断是否已经存在当前用户
        /// </summary>
        /// <param name="connString">数据库连接字符串</param>
        /// <param name="userName">用户名称</param>
        /// <param name="departmentID">部门名称</param>
        /// <returns>是否存在</returns>
        public bool ExistUser(string connString, string userName, int departmentID)
        {
            //检测用户是否为空
            if (string.IsNullOrEmpty(userName))
            {
                throw new Exception("用户不能为空");
            }
            try
            {
                return dao.ExistUser(connString, userName, departmentID);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 使用SqlBulkCopy批量插入部门用户
        /// </summary>
        /// <param name="connString">数据库连接字符串</param>
        /// <param name="usrsDT">批量插入数据</param>
        /// <returns></returns>
        public bool ImportUser(string connString, DataTable usrsDT) 
        {            
            try
            {
                return dao.ImportUser(connString, usrsDT);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

    }
}
